package com.js.mytransitiondemo.adapter;

// Created by JS on 2021/7/6.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.js.mytransitiondemo.R;
import com.js.mytransitiondemo.model.Effect;

import java.util.List;

public class EffectListAdapter extends RecyclerView.Adapter<EffectListAdapter.ViewHolder> {

    private Context mContext;
    private List<Effect> mList;
    private OnItemClickListener mListener;

    public EffectListAdapter(Context context, List<Effect> list, OnItemClickListener listener) {
        this.mContext = context;
        this.mList = list;
        this.mListener = listener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_after_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull EffectListAdapter.ViewHolder holder, int position) {
        Effect effect = mList.get(position);
        holder.textView.setText(effect.getName());
        Glide.with(mContext)
                .load(effect.getUri())
                .apply(new RequestOptions().skipMemoryCache(true))
                .into(holder.imageView);
        holder.imageView.setOnClickListener(v -> mListener.onItemClick(effect, holder.imageView));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public interface OnItemClickListener {
        void onItemClick(Effect effect, View view);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.item_name);
            imageView = itemView.findViewById(R.id.item_img);
        }
    }
}
